<template>
	<view class="add-address-box fz15">
		<view class="address-line flex align-c">
			<view class="w60 mr20">
				收货人
			</view>
			<u-input v-model="consignee" placeholder-style='color:#999' placeholder='请输入名字' :clearable="false" type="text" />
		</view>
		<view class="address-line flex align-c">
			<view class="w60 mr20">
				手机号码
			</view>
			<u-input v-model="phone" placeholder-style='color:#999' maxlength='11' placeholder='请输入手机号' :clearable="false"
				type="text" />
		</view>
		<view class="address-line flex align-c">
			<view class="w60 mr20">
				所在地区
			</view>
			<view class="c9 address-line-right" @click="is_show_address=true" v-if="select_address.length <= 0">省、市、区</view>
			<view v-else class="address-line-right flex align-c" @click="is_show_address=true">
				{{showSelectAddress}}
			</view>
		</view>
		<view class="address-line flex align-c">
			<view class="w60 mr20">
				详细地址
			</view>
			<u-input v-model="detailed_address" placeholder-style='color:#999' placeholder='街道、小区楼栋/乡村名称' :clearable="false"
				type="text" />
		</view>
		<view class="address-line space-between">
			<view class="">
				设为默认地址
			</view>
			<u-switch v-model="is_default" size='36' active-color='#B37816'></u-switch>
		</view>
		<!-- 选择省市区 -->
		<u-select v-model="is_show_address" mode="mutil-column-auto" :default-value="is_default_select" :list="list"
			@confirm="confirm"></u-select>
		<!-- 确认 -->
		<u-button class="save-btn-box" @click="jumpPath" :throttle-time='3000'>保存</u-button>
	</view>
</template>

<script>
	import {
		getAreaList,
		editAddredss,
		getAddressInfo,
	} from '@/network/loginAndMy.js'
	export default {
		data() {
			return {
				consignee: '', // 收货人
				phone: '', // 手机号
				detailed_address: '', // 详细地址
				is_default: false, // 是否默认
				is_show_address: false, // 是否显示地区选择
				id: '', // 编辑地址id
				list: [], // 省市区
				select_address: [], // 选择省市区
				is_default_select: [], // 是否默认选择地区
			}
		},
		onLoad(option) {
			if (option.id != undefined) {
				this.id = option.id
				this.getAddressInfo()
				uni.setNavigationBarTitle({
					title: '编辑地址'
				});
			}else {
				this.getAreaList()
			}
		},
		computed: {
			showSelectAddress() {
				let shwo_text_address = ''
				this.select_address.forEach((item, index) => {
					shwo_text_address = `${shwo_text_address}${item.label}${index<this.select_address.length-1?'、':''}`
				})
				return shwo_text_address
			}
		},
		methods: {
			// 确认省市区
			confirm(value) {
				this.select_address = value
			},
			// 跳转页面
			jumpPath() {
				if (this.contentVerify()) return

				let params = {
					id: this.id,
					is_default: this.is_default ? 1 : 0,
					consignee: this.consignee,
					phone: this.phone,
					area_text: `${this.select_address[0].label}-${this.select_address[1].label}-${this.select_address[2].label}`,
					address: this.detailed_address,
				}

				editAddredss(params).then(res => {
					if (res.code == 1) {
						uni.navigateBack({
							delta: 1
						});
					}
				})
			},
			// 获取地区列表
			getAreaList(type) {
				getAreaList().then(res => {
					if (res.code == 1) {
						let address_list = res.data.provinceData
						address_list.forEach((item, index) => {
							if(type == 1 && this.select_address[0].label == item.label){
								this.is_default_select.push(index)
							}
							
							item.children = res.data.cityData[index]
							
							item.children.forEach((item_two, index_two) => {
								item_two.children = res.data.areaData[index][index_two]
								
								if(type == 1 && this.select_address[1].label == item_two.label){
									this.is_default_select.push(index_two)
								}
								if(type == 1){
									res.data.areaData[index][index_two].forEach((item_three,index_three)=>{
										if(this.select_address[2].label == item_three.label){
											this.is_default_select.push(index_three)
										}
									})
								}
							})
						})
						this.list = address_list
					}
				})
			},
			// 获取地址信息
			getAddressInfo() {
				getAddressInfo({
					id: this.id
				}).then(res => {
					this.consignee = res.data.consignee
					this.phone = res.data.phone
					this.detailed_address = res.data.address
					this.is_default = Number(res.data.is_default) ? true : false
					this.select_address = [{
						label: res.data.province_name,
						value: res.data.province_id
					}, {
						label: res.data.city_name,
						value: res.data.city_id
					}, {
						label: res.data.area_name,
						value: res.data.area_id
					}]
					this.getAreaList(1)
				})
			},
			// 输入内容验证
			contentVerify() {
				if (this.consignee == '') {
					uni.showToast({
						title: '请输入收货人姓名',
						duration: 2000,
						icon: 'none'
					});
					return true
				}

				if (this.phone == '') {
					uni.showToast({
						title: '请输入手机号',
						duration: 2000,
						icon: 'none'
					});
					return true
				}

				let regu = /^1[3-9][0-9]{9}$/
				if (!regu.test(this.phone)) {
					uni.showToast({
						title: '请输入有效的手机号码',
						duration: 2000,
						icon: 'none'
					});
					return true
				}

				if (this.select_address.length <= 0) {
					uni.showToast({
						title: '请选择收货地址',
						duration: 2000,
						icon: 'none'
					});
					return true
				}

				if (this.detailed_address == '') {
					uni.showToast({
						title: '请输入详细地址',
						duration: 2000,
						icon: 'none'
					});
					return true
				}

				return false
			},
		},
		onUnload() {
			uni.$off('addressInfo')
		},
	}
</script>

<style lang="scss">
	.add-address-box {
		background-color: #F3F3F3;
		min-height: 100vh;
		padding-top: 20rpx;
		padding-bottom: 20rpx;

		.address-line {
			background-color: #ffffff;
			position: relative;
			min-height: 90rpx;
			padding: 0 30rpx;

			.address-line-right {
				width: calc(100vw - 220rpx);
				overflow: hidden; //超出文本隐藏
				text-overflow: ellipsis; ///超出部分省略号显示
				display: -webkit-box; //弹性盒模型
				-webkit-box-orient: vertical; //上下垂直
				-webkit-line-clamp: 1; //自定义行数
			}
		}

		.address-line::after {
			content: "";
			width: 690rpx;
			height: 2rpx;
			background-color: #F2F2F2;
			position: absolute;
			bottom: 0;
			left: 50%;
			transform: translateX(-50%);
		}

		.address-line:last-child::after {
			content: "";
			width: 690rpx;
			height: 2rpx;
			background-color: transparent;
			position: absolute;
			bottom: 0;
			left: 50%;
			transform: translateX(-50%);
		}

		.save-btn-box {
			width: 690rpx;
			height: 90rpx;
			border-radius: 5rpx;
			background: linear-gradient(0deg, #b07412 0%, #e3ba5b 100%);
			line-height: 90rpx;
			text-align: center;
			color: #ffffff;
			position: fixed;
			bottom: 20rpx;
			left: 50%;
			transform: translateX(-50%);
			padding: 0 !important;
			margin: 0 !important;
		}
		uni-button:after {
			content: " ";
			border: none !important;
		}
	}
</style>